package com.ifengchao.dao.impl;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.ifengchao.dao.iface.DealReptileDao;
import com.ifengchao.dao.IBatisBaseDao1;
import com.ifengchao.pojo.HConfig;
import com.ifengchao.pojo.HCrawlLog;
import com.ifengchao.pojo.HMessagePlat;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author YiMing
 * @Description:
 * @Date: Created in 下午5:11 2017/9/11
 * @Modified By
 */
@Repository
public class DealReptileDaoImpl implements DealReptileDao{

	private static final Logger logger = Logger.getLogger(DealReptileDao.class);

	@Autowired(required=true)
	private IBatisBaseDao1 iBatisBaseDao1;

	@Override
	public void insertMessage(List<HMessagePlat> hMessagePlats) {
		logger.info("批量插入数据");
		List<HMessagePlat> tempList = hMessagePlats;
		for (HMessagePlat hMessagePlat : tempList) {
			try {
				iBatisBaseDao1.getSqlMapClientTemplate().insert("dealReptileSql.insertMessage", hMessagePlat);
			} catch (Exception e) {
				logger.error("=====相同的文章,跳过不插入=====",e);
			}
		}
		/*try {
			if (tempList != null && tempList.size() > 0) {
				iBatisBaseDao1.getSqlMapClientTemplate().execute(
						new SqlMapClientCallback() {
							public Object doInSqlMapClient(SqlMapExecutor executor) throws
									SQLException {
								try {
									executor.startBatch();
									for (int i = 0, n = tempList.size(); i < n; i++) {
										executor.insert("dealReptileSql.insertMessage", tempList.get(i));
									}
									executor.executeBatch();
								} catch (Exception e) {
									logger.error("=====相同的文章,跳过不插入=====",e);
								}
								return null;
							}
						}
				);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/
	}

	@Override
	public List<HConfig> queryConfig() {
		logger.info("查询爬虫网站配置表数据");
		return iBatisBaseDao1.getSqlMapClientTemplate().queryForList("dealReptileSql.queryConfig");
	}

	@Override
	public String queryMenu(String menuNo) {
		logger.info("查询资讯表数据");
		Object o = iBatisBaseDao1.getSqlMapClientTemplate().queryForObject("dealReptileSql.queryMenu", menuNo);
		if (o != null) {
			return o.toString();
		}
		return null;
	}

	@Override
	public List<String> queryTitle(String platNo) {
		logger.info("查询标题");
		return iBatisBaseDao1.getSqlMapClientTemplate().queryForList("dealReptileSql.queryTitle",platNo);
	}

	@Override
	public void insertWeChatMessage(HMessagePlat hMessagePlat) {
		logger.info("开始插入数据");
		try {
			iBatisBaseDao1.getSqlMapClientTemplate().insert("dealReptileSql.insertMessage", hMessagePlat);
		} catch (Exception e) {
			logger.error("=====相同的文章,跳过不插入=====",e);
		}
	}

	/*@Override
	public void clearMessage(String platNo) {
		logger.info("先查询在移动到历史表中");
		final List<HMessagePlat> tempList = iBatisBaseDao1.getSqlMapClientTemplate().queryForList("dealReptileSql.queryMessagePlat", platNo);
		try {
			if (tempList != null && tempList.size() > 0) {
				iBatisBaseDao1.getSqlMapClientTemplate().execute(
						new SqlMapClientCallback() {
							public Object doInSqlMapClient(SqlMapExecutor executor) throws
									SQLException {
								executor.startBatch();
								for (int i = 0, n = tempList.size(); i < n; i++) {
									executor.insert("dealReptileSql.insertMessageHistory", tempList.get(i));
								}
								executor.executeBatch();
								return null;
							}
						}
				);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

	@Override
	public Map<String,String> querySetting() {
		logger.info("查询爬虫开关");
		return iBatisBaseDao1.getSqlMapClientTemplate().queryForMap("dealReptileSql.querySetting",new HashMap<String,String>(),"name","value");
	}

	@Override
	public void insertSpecialMessage(List<HMessagePlat> hMessagePlats) {
		logger.info("批量插入新的数据前删除旧数据");
		List<HMessagePlat> tempList = hMessagePlats;
		//String platNo = tempList.get(0).getPlatNo();
		//iBatisBaseDao1.getSqlMapClientTemplate().delete("dealReptileSql.deleteMessage", platNo);
		for (int i = 0, n = tempList.size(); i < n; i++) {
			try {
				iBatisBaseDao1.getSqlMapClientTemplate().insert("dealReptileSql.insertMessage", tempList.get(i));
			} catch (Exception e) {
				logger.info("==========相同的文章不插入跳过======");
			}
		}
			/*try {
			if (tempList != null && tempList.size() > 0) {
				iBatisBaseDao1.getSqlMapClientTemplate().execute(
						new SqlMapClientCallback() {
							public Object doInSqlMapClient(SqlMapExecutor executor) throws
									SQLException {
								executor.startBatch();
								String platNo = tempList.get(0).getPlatNo();
								executor.delete("dealReptileSql.deleteMessage", platNo);
								for (int i = 0, n = tempList.size(); i < n; i++) {
									executor.insert("dealReptileSql.insertMessage", tempList.get(i));
								}
								executor.executeBatch();
								return null;
							}
						}
				);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/
	}

	@Override
	public void insertCrawlLog(ArrayList<HCrawlLog> hCrawlLogs) {
		logger.info("插入爬虫记录表");
		final List<HCrawlLog> tempList = hCrawlLogs;
		try {
			if (tempList != null && tempList.size() > 0) {
				iBatisBaseDao1.getSqlMapClientTemplate().execute(
						new SqlMapClientCallback() {
							public Object doInSqlMapClient(SqlMapExecutor executor) throws
									SQLException {
								executor.startBatch();
								for (int i = 0, n = tempList.size(); i < n; i++) {
									executor.insert("dealReptileSql.insertCrawlLog", tempList.get(i));
								}
								executor.executeBatch();
								return null;
							}
						}
				);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
